package com.lhm.service;



import com.lhm.entity.MyUser;
import com.lhm.entity.User;
import com.lhm.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Primary
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    UserMapper userMapper;

    @Autowired
    PasswordEncoder passwordEncoder;



    @Override
    public MyUser findByAccount(String account) {
        return userMapper.findByAccount(account);
    }

    @Override
    public int update(Map<String, Object> map) {
        String password = (String)map.get("password");
        if (password != null && password.length() > 0) {
            password = this.passwordEncoder.encode(password);
            map.put("password", password);
        }

        this.userMapper.update(map);
        return 0;

    }

    @Override
    public int getCount() {
        return userMapper.getCount();
    }

    @Override
    public MyUser findById(int id) {
        return userMapper.findById(id);
    }

    @Override
    public void add(MyUser u) {
        String pass = u.getPassword();
        if (pass != null || pass.length() > 1) {
            u.setPassword(this.passwordEncoder.encode(pass));
        }

        this.userMapper.add(u);
    }

    @Override
    public List<Map<String, Object>> findAll(Integer start, Integer num) {
        Map<String, Object> map = new HashMap();
        map.put("start", start);
        map.put("num", num);
        return this.userMapper.findAll(map);
    }

}



